package com.struts.deal.admin.news;


import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;

import com.base.Constants;
import com.ibatis.po.system.TNews;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.lg.system.CSqlMap;
import com.lg.system.CTurnPage;
import com.lg.system.Common;
import com.lg.system.MessageBean;
import com.struts.web.manage.admin.news.EditNewsIssueForm;
import com.struts.web.manage.admin.news.NewsIssueForm;
import com.struts.web.manage.admin.news.ViewNewsIssueForm;

public class NewsDeal {


	
	/**
	 * 新闻列表浏览
	 * @param request
	 * @param aWebForm
	 */
	public static void viewNewsIssue(HttpServletRequest request,ViewNewsIssueForm aWebForm){

		CTurnPage page = new CTurnPage();
		TNews newsPo = new TNews();
		try{
			if(aWebForm.getNewsTitle()!=null && aWebForm.getNewsTitle()!=""){
				newsPo.setTitle(aWebForm.getNewsTitle());
				
			}
			if(aWebForm.getNewsType()!=null && aWebForm.getNewsType()!=""){
				newsPo.setKindOfNewsId(aWebForm.getNewsType());
			}
			
			page.setQueryList("T_NEWS.selectAllNews",newsPo);
			page.setPerPageRows(10);
			page.excute(request);
			request.setAttribute("page", page);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	/**
	 * 新闻发布
	 * @param request
	 * @param aWebForm
	 */
	public static MessageBean addNews(HttpServletRequest request,NewsIssueForm aWebForm) throws SQLException{
		MessageBean messageBean = new MessageBean();
		TNews newsPo = new TNews();
		SqlMapClient sqlmap = CSqlMap.getSqlMapConfig();
		String newsInforsource = "职介所";//如果页面中信息来源为空，则默认为“职介所”
		try{
			if(aWebForm.getInfoSourse()!=null && aWebForm.getInfoSourse()!=""){
				newsInforsource = aWebForm.getInfoSourse();
			}
			
			newsPo.setId(Common.getNewID());
			newsPo.setTitle(aWebForm.getNewsTitle());
			newsPo.setKindOfNewsId(aWebForm.getNewsType());
			newsPo.setInfoSource(newsInforsource);
			newsPo.setContent(request.getParameter("newsContent"));
			newsPo.setIssueDate(new java.util.Date());
			newsPo.setViewTime("1");
			sqlmap.insert("T_NEWS.abatorgenerated_insert",newsPo);
			messageBean.setResult(true);
			messageBean.setMessage(Constants.ADD_SUC);
		}catch(Exception e){
			e.printStackTrace();
		}
		return messageBean;
	}
	
	/**
	 * 新闻修改
	 * @param request
	 * @param aWebForm
	 */
	public static MessageBean editNews(HttpServletRequest request,EditNewsIssueForm aWebForm) throws SQLException{
		MessageBean messageBean = new MessageBean();
		TNews newsPo = new TNews();
		SqlMapClient sqlmap = CSqlMap.getSqlMapConfig();
		try{
			newsPo.setId(aWebForm.getNewsId());
			newsPo.setTitle(aWebForm.getNewsTitle());
			newsPo.setKindOfNewsId(aWebForm.getNewsType());
			newsPo.setInfoSource(aWebForm.getInfoSourse());
			newsPo.setContent(request.getParameter("newsContent"));
			sqlmap.update("T_NEWS.abatorgenerated_updateByPrimaryKeySelective",newsPo);
			messageBean.setResult(true);
			messageBean.setMessage(Constants.EDIT_SUC);
		}catch(Exception e){
			e.printStackTrace();
		}
		return messageBean;
	}
	
	/**
	 * 删除新闻
	 * @param request
	 * @param aWebForm
	 */
	public static MessageBean delNews(HttpServletRequest request,EditNewsIssueForm aWebForm) throws SQLException{
		MessageBean messageBean = new MessageBean();
		TNews newsPo = new TNews();
		SqlMapClient sqlmap = CSqlMap.getSqlMapConfig();
		try{
			newsPo.setId(aWebForm.getNewsId());
			sqlmap.delete("T_NEWS.abatorgenerated_deleteByPrimaryKey",newsPo);
			messageBean.setResult(true);
			messageBean.setMessage(Constants.DEL_SUCCESS);
		}catch(Exception e){
			e.printStackTrace();
		}
		return messageBean;
	}
	
	/**
	 * 加载新闻信息
	 * @param request
	 * @param aWebForm
	 */
	public static MessageBean loadEditNews(HttpServletRequest request,EditNewsIssueForm aWebForm) throws SQLException{
		TNews newsPo = new TNews();
		MessageBean messageBean = new MessageBean();
		String newsId = request.getParameter("newsId");
		SqlMapClient sqlmap = CSqlMap.getSqlMapConfig();
		try{
			newsPo.setId(newsId);
			newsPo = (TNews)sqlmap.queryForObject("T_NEWS.abatorgenerated_selectByPrimaryKey",newsPo);
			if(newsPo!=null){
				aWebForm.setNewsTitle(newsPo.getTitle());
				aWebForm.setNewsId(newsId);
				aWebForm.setNewsType(newsPo.getKindOfNewsId());
				aWebForm.setTempNewsContent(newsPo.getContent());
				aWebForm.setInfoSourse(newsPo.getInfoSource());
				messageBean.setResult(true);
			}else{
				messageBean.setResult(false);
				messageBean.setMessage(Constants.LOAD_FAIL);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return messageBean;
	}
}
